package com.apes.fn.fico.debitnote.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.debitnote.model
 * @ClassName: LoanNote
 * @Author: pyb
 * @Description: 借支放款单
 * @CreateTime: 2021-01-13 14:40
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LoanNote.CONST_TABLE_NAME)
public class LoanNote extends Model {
    public static final String CONST_TABLE_NAME = "fn_loan_note";

    @Id
    @Column(name = "id", length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "借支放款单", padding = 10)
    @Comment("借支放款单编码")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码(平台)")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_id", referencedColumnName = "id", nullable = false)
    @Comment("赋能商户")
    private PartyRole fnCustomer;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "制单"),
            @State(id = "approve", name = "审核"),
    })
    @Column(name = "state", length = 36, nullable = false)
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate;

    @Column(name = "remark", length = 250)
    @Comment("备注")
    private String remark;

    @OneToMany(mappedBy = "loanNote", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<LoanNoteItem> loanNoteItemList=new ArrayList<>();

    public List<LoanNoteItem> getLoanNoteItemList(){
        for (LoanNoteItem loanNoteItem : loanNoteItemList) {
            loanNoteItem.setLoanNote(this);
        }
        return loanNoteItemList;
    }

}
